package com.zh.websocket;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.zh.domain.Model.ws.TableMessageModel;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

public class TableMessageDecoder implements Decoder.Text<TableMessageModel> {
    private static final ObjectMapper mapper = new ObjectMapper();

    static {
        // 注册 JavaTimeModule 以支持 Java 8 日期/时间类型
        mapper.registerModule(new JavaTimeModule());
        // 忽略未知字段（可选）
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    @Override
    public TableMessageModel decode(String s) throws DecodeException {
        try {
            return mapper.readValue(s, TableMessageModel.class);
        } catch (Exception e) {
            throw new DecodeException(s, "Failed to decode TableMessageModel", e);
        }
    }

    @Override
    public boolean willDecode(String s) {
        return true;
    }

    @Override
    public void init(EndpointConfig config) {
        // 初始化配置，如果需要的话
    }

    @Override
    public void destroy() {
        // 清理资源，如果需要的话
    }
}